package cn.iocoder.yudao.module.shop.controller.admin.opthistory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagRespVO;
import cn.iocoder.yudao.module.blog.convert.jiutag.BlogJiuTagConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.blog.service.jiutag.BlogJiuTagService;
import cn.iocoder.yudao.module.shop.controller.admin.opthistory.vo.ShopOptHistoryCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.opthistory.vo.ShopOptHistoryPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.opthistory.vo.ShopOptHistoryRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.order.vo.ShopOrderRespVO;
import cn.iocoder.yudao.module.shop.convert.opthistory.ShopOptHistoryConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.opthistory.ShopOptHistoryDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;
import cn.iocoder.yudao.module.shop.service.opthistory.ShopOptHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

@Tag(name = "管理后台 - 店内操作记录")
@RestController
@RequestMapping("/shop/opthistory")
@Validated
public class ShopOptHistoryController {

    @Resource
    private ShopOptHistoryService shopOptHistoryService;

    @Resource
    private BlogJiuTagService jiuTagService;

    @PostMapping("/create")
    @Operation(summary = "创建案例")
    public CommonResult<Long> createMemberPay(@Valid @RequestBody ShopOptHistoryCreateReqVO createReqVO) {
        return success(shopOptHistoryService.create(createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得操作记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ShopOptHistoryRespVO> get(@RequestParam("id") Long id){
        ShopOptHistoryDO shopOptHistoryDO = shopOptHistoryService.get(id);

        Set<Long> tagIds = new HashSet<>();
        if(shopOptHistoryDO.getTags() != null && StringUtil.isNotEmpty(shopOptHistoryDO.getTags())){
            String[] ids = shopOptHistoryDO.getTags().split(";");
            for(int i = 0; i<ids.length ; i++){
                tagIds.add(Long.parseLong(ids[i]));
            }
        }

        List<BlogJiuTagDO> tagList = jiuTagService.getJiuTagList(tagIds);

        return success(ShopOptHistoryConvert.INSTANCE.convert(shopOptHistoryDO, tagList));
    }

    @PostMapping("/tag/create")
    @Operation(summary = "创建案例标签")
    public CommonResult<Long> createJiuTag(@Valid @RequestBody BlogJiuTagCreateReqVO createReqVO) {
        return success(jiuTagService.createJiuTag(createReqVO));
    }

    @GetMapping("/tag/all")
    @Operation(summary = "获取所有标签")
    public CommonResult<List<BlogJiuTagRespVO>> getAll(){
        return success(BlogJiuTagConvert.INSTANCE.convertList(jiuTagService.getAllTags()));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例分页")
    public CommonResult<PageResult<ShopOptHistoryRespVO>> getOrderPage(@Valid ShopOptHistoryPageReqVO pageVO) {
        PageResult<ShopOptHistoryDO> pageResult = shopOptHistoryService.getHistoryPage(pageVO);
        if(CollUtil.isEmpty(pageResult.getList())){
            return success(PageResult.empty());
        }

        Set<Long> tagIds = new HashSet<>();
        for(ShopOptHistoryDO item : pageResult.getList()){
            if(item.getTags() != null && StringUtil.isNotEmpty(item.getTags())){
                String[] ids = item.getTags().split(";");
                for(int i = 0; i<ids.length ; i++){
                    tagIds.add(Long.parseLong(ids[i]));
                }
            }
        }
        List<BlogJiuTagDO> tagList = jiuTagService.getJiuTagList(tagIds);
        return success(ShopOptHistoryConvert.INSTANCE.convertPage(pageResult, tagList));
    }
}
